import * as path from 'path';

/**
 * 获取项目根目录
 */
function getProjectRoot(): string {
  // 从当前文件位置向上查找，直到找到 package.json
  let currentDir = __dirname;
  while (currentDir !== path.dirname(currentDir)) {
    try {
      const packageJsonPath = path.join(currentDir, 'package.json');
      require.resolve(packageJsonPath);
      return currentDir;
    } catch {
      currentDir = path.dirname(currentDir);
    }
  }
  // 如果找不到，使用相对路径作为后备
  return path.join(__dirname, '../..');
}

/**
 * 获取录制文件的完整路径
 * @param filename 文件名
 * @returns 完整的文件路径
 */
export function getRecordingsPath(filename: string): string {
  const recordingsDir = process.env.RECORDINGS_PATH || path.join(getProjectRoot(), 'recordings');
  return path.normalize(path.join(recordingsDir, filename));
}

/**
 * 获取录制目录路径
 * @returns 录制目录路径
 */
export function getRecordingsDir(): string {
  const recordingsPath = process.env.RECORDINGS_PATH || path.join(getProjectRoot(), 'recordings');
  return path.normalize(recordingsPath);
}